import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.ticker as ticker

# 设置中文显示
plt.rcParams['font.sans-serif'] = ['SimHei']

# 1. 创建2个列表：城市名称address_list和平均工资空列表city_ai_salary
city_ai_salary = []
address_list = ["成都", "西安", "北京", "杭州", "上海", "南京", "武汉", "深圳", "广州"]

# 2. for语句，依次读取各个城市工资数据，并计算平均值salary_average
for address in address_list:
    data = pd.read_excel('E:\pythonstudy\pythonProject1\data4\data4_4_classify_address\人工智能成都.xlsx')
    data = data.drop('Unnamed: 0', axis=1)
    salary_average = int(data['salary'].mean())

    # 3. 将各个城市平均工资值追加到city_ai_salary中
    print("这是{}的人工智能数据的平均工资{}".format(address, salary_average))
    city_ai_salary.append(salary_average)

# 4. 将工资平均值和城市名称列表拼接在一起
data_new = pd.DataFrame({
    "城市名称": address_list,
    "平均工资": city_ai_salary
})

# 5. 按照工资对数据进行重新排序
data_new = data_new.sort_values(by='平均工资', ascending=False)
data_new = data_new.reset_index(drop=True)

# 6. 显示柱状图
colors = ['g', 'r', 'b', 'c','m', 'y', 'k', 'orange', 'purple', 'brown']
data_new.plot(kind='bar', x='城市名称', y='平均工资', width=0.5, color=colors)

# 7. 柱状图上数字显示
average_salary = data_new['平均工资'].tolist()
for x, y in enumerate(average_salary):
    plt.text(x, y, y, ha='center', fontsize=12)

# 8. 图标的显示配置和保存
plt.ylim(12000, 20000)
plt.title("全国城市人工智能岗位平均工资排序")
plt.xlabel("城市名称", fontsize=16)
plt.ylabel("平均工资", fontsize=16)
plt.xticks(fontsize=10, rotation=0)
plt.tight_layout()
# plt.figure(figsize=(12, 6))
plt.savefig('./picture/5 - 2.全国城市人工智能岗位平均工资排序.png')
plt.show()